﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Models;

namespace DAL
{
    public class CharService
    {
        /// <summary>
        /// 获取全部角色
        /// </summary>
        public int pageSize = 17;//每页显示的行
        public int pageCurrent = 0;//必须从0开始
        public DataSet GetAllChar()
        {
            DataSet ds = new DataSet();
            try
            {
                string sql = string.Format("select account_name,objid,char_name,level,maxhp,maxmp,ac,case sex when 0 THEN '男' else '女' end as sex,case type when 0 THEN '王族' when 1 then '骑士' when 2 then '妖精' when 3 then '法师' when 4 then '黑妖' when 5 then '龙骑士' else '幻术师' end as type FROM characters LIMIT {0},{1}", pageCurrent, pageSize);
                MySqlDataAdapter objAda = new MySqlDataAdapter(sql, DBHelper.con);
                objAda.Fill(ds, "CharTable");
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            return ds;
        }

        public DataSet GetSearchChar()//查询
        {
            DataSet ds = new DataSet();
            try
            {
                string sql = "select account_name,objid,char_name,level,maxhp,maxmp,ac,case sex when 0 THEN '男' else '女' end as sex,case type when 0 THEN '王族' when 1 then '骑士' when 2 then '妖精' when 1 then '法师' when 1 then '黑妖' when 1 then '龙骑士' else '幻术师' end as type FROM characters";
                MySqlDataAdapter objAda = new MySqlDataAdapter(sql, DBHelper.con);
                objAda.Fill(ds, "SearchCharTable");
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            return ds;
        }

        public int Account(string account, string pwd, int AccType)
        {
            int num = 0;
            try
            {
                string sql = string.Empty;
                switch (AccType)
                {
                    case 1://修改密码
                        sql = string.Format("update accounts set Password='{0}' where login='{1}'", pwd, account);
                        break;
                    case 2://新增帐号
                        sql = string.Format("insert into accounts values('{0}','{1}',now(),0,0,0,0,0,0)", account, pwd);
                        break;
                }
                MySqlCommand com = new MySqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                num = Convert.ToInt32(com.ExecuteNonQuery());
            }
            //catch (MySqlException ex)
            //{
            //    string error = string.Empty;
            //    if (ex.Number == 1582)//主键重复
            //    {
            //        error = "账号已存在";
            //    }
            //    throw new Exception(error);
            //}
            catch (MySqlException ex)
            {
                return ex.Number;
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            if (num == 0)
            {
                return 0;
            }
            return 1;
        }
    }
}
